package com.ruoyi.web.controller.diagmanage;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Page;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.ruoyi.analysiswarning.domain.vo.PageVO;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.diagmanage.domain.DiaIntelligent;
import com.ruoyi.diagmanage.domain.Intelligent;
import com.ruoyi.diagmanage.domain.StateInventory;
import com.ruoyi.diagmanage.domain.StateReport;
import com.ruoyi.diagmanage.domain.vo.ReportExcelVo;
import com.ruoyi.diagmanage.service.StateReportService;
import com.ruoyi.web.controller.util.Export2ExcelListMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/StateReportController")
public class StateReportController {


    @Autowired
    private StateReportService stateReportService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }

    @Log(title = "问题统计查询", businessType = BusinessType.QUERY)
    @PostMapping("/queryStateReportList")
    public AjaxResult queryStateReport(@RequestBody StateReport stateReport)
    {
        List<StateReport> stateReports = stateReportService.queryReportList(stateReport);
        return success(stateReports);
    }

    @Log(title = "巡检报告清单查询", businessType = BusinessType.QUERY)
    @PostMapping("/queryInventoryList")
    public AjaxResult queryInventoryList(@RequestBody PageVO<StateInventory> pageVO) {
        return success(stateReportService.queryInventoryList(pageVO));
    }




    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")//StateInventory stateInventory
    public void export(HttpServletResponse response,@RequestBody List<Map<String,Object>> dataList) throws IOException {

        //表头 如果需要改动表头样式与实体类方法一致
        List<List<String>> heads = new ArrayList<>();
        heads.add(Arrays.asList("系统名称"));
        heads.add(Arrays.asList("巡检开始时间"));
        heads.add(Arrays.asList("问题描述"));
        heads.add(Arrays.asList("问题状态"));
        //内容 这里使用List<Map<String, Object>> 如果要用实体类就要转换或者改service 太麻烦
        //List<Map<String, String>> list = stateReportService.selectReportQuestion(reportId);
        List<List<Object>> total=new ArrayList<>();
        for(Map<String, Object> map : dataList){
            List<Object> info=new ArrayList<>();
            info.add(map.get("systemName"));
            info.add(map.get("beginTime"));
            info.add(map.get("baseInfomation"));
            boolean seriousState = ObjectUtil.isNotEmpty(map.get("seriousState"));
            info.add( seriousState ? "1".equals(map.get("seriousState")) ? "严重" : "预警" : "");
            total.add(info);
        }

        String fileName = URLEncoder.encode("巡检报告", "UTF-8").replaceAll("\\+", "%20");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");//设置响应头
        response.setHeader("mime","application/vnd.ms-excel");

        EasyExcel.write(response.getOutputStream())
                .head(heads)
                .sheet("巡检报告")
                .doWrite(total);

    }


    /*@PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")//StateInventory stateInventory
    public void export(HttpServletResponse response, String reportId) {

        List<StateInventory> stateInventories = stateReportService.queryInventoryList(stateInventory);
        for(StateInventory x : stateInventories){
            if (x.getState().equals("2")){
                x.setState("巡检中");
                x.setFinishTime("-");
                x.setQuestionCount("-");
                x.setSeriousCount("-");
            }
            if (x.getState().equals("1")){
                x.setState("未巡检");
                x.setBeginTime("-");
                x.setFinishTime("-");
                x.setQuestionCount("-");
                x.setSeriousCount("-");
            }
            if (x.getState().equals("3")){
                x.setState("已巡检");
            }
        }

        ExcelUtil<StateInventory> util = new ExcelUtil<StateInventory>(StateInventory.class);

        util.exportExcel(response, stateInventories, "状态巡检分析结果");
    }*/


    @Log(title = "通过dbId查询系统信息", businessType = BusinessType.QUERY)
    @PostMapping("/getSystemByDBId")
    public AjaxResult getSystemByDBId(String reportId) {
        return success(stateReportService.selectReportQuestion(reportId));
    }





}
